var _ = require('lodash'),
    Q = require('q'),
    CBOEnum = require('../../../CBO/server/controllers/Enum.server.controller'),
    departmentController = require('../../../CBO/server/controllers/Department.server.controller'),
    workLocationController = require('../../../CBO/server/controllers/WorkLocation.server.controller'),
    stationWorkController = require('../../../Initialize/server/controllers/StationWork.server.controller'),
    stationTerminalController = require('../../../Initialize/server/controllers/StationTerminal.server.controller'),
    deviceMonitorController = require('../../../CBO/server/controllers/DeviceMonitor.server.controller');

exports.getDptReaderState = function (req, res) {
    var _dpt = req.body.Department,
        _workLocationIds = [],
        _workLocationObj = {}, 
        _workObj = {},
        _exceptionDevices = [];
    departmentController.findSubDepartmentsDeep(_dpt).then(function (dpts) {
        dpts.push(_dpt);
        return workLocationController.findDepartmentStations(dpts, 'Code Name');
    }).then(function (workLocations) {
        _.forEach(workLocations, function (workLocation) {
            _workLocationObj[workLocation._id] = workLocation;
            _workLocationIds.push(workLocation._id);
        });
        return stationWorkController.findByWorkLocationIDs(_workLocationIds, null, {path: 'Work', select: 'Code Name'});
    }).then(function (stationWorks) {
        _.forEach(stationWorks, function (stationWork) {
            _workObj[stationWork.WorkLocation] = stationWork.Work;
        });
        return deviceMonitorController.getExceptionDevices();
    }).then(function (exceptionDevices) {
        _exceptionDevices = exceptionDevices;
        return stationTerminalController.findByWorkLocationIDs(_workLocationIds, null, {path: 'Terminal', select: 'Code Name UniqueID Type LastRequestDate'});
    }).then(function (stationTerminals) {
        var results = [],
            exceptionDevices = _exceptionDevices,
            nowTime = new Date();
        _.forEach(stationTerminals, function (stationTerminal) {
            if (stationTerminal.Terminal.Type === CBOEnum.TerminalType.WiFi.Value) {
                var lastRequestDate = stationTerminal.Terminal.LastRequestDate,
                    terminalStatus = 0; // 正常
                if (!lastRequestDate || (nowTime - lastRequestDate) >= 120000) {
                    terminalStatus = 2; // 连接异常
                } else if (_.indexOf(exceptionDevices, stationTerminal.Terminal.UniqueID) !== -1) {
                    terminalStatus = 1; // 解析异常
                }
                results.push({
                    Terminal: stationTerminal.Terminal,
                    WorkLocation: _workLocationObj[stationTerminal.WorkLocation],
                    Work: _workObj[stationTerminal.WorkLocation],
                    status: terminalStatus
                });
            }
        });
        res.json({
            Data: results,
            Error: null
        });
    }).catch(function (err) {
        res.json({
            Data: null,
            Error: err
        });
    })
    .done();
};
